package model

import "github.com/gin-gonic/gin"

type MajorDictionary struct {
	MNo                 string `gorm:"column:major_no"`
	MName               string `gorm:"column:major_name"`
	PlannedEnrollment   string `gorm:"column:planned_enrollment"`
	UnPlannedEnrollment string `gorm:"column:unplanned_enrollment"`
}

func GetMajorDic(majorparam map[string]interface{}) (error, []MajorDictionary, int64) {
	var major []MajorDictionary
	page := majorparam["page"].(int)
	pageSize := majorparam["limit"].(int)
	searchText := majorparam["searchText"].(string)
	var total int64
	err := DB.Table("major_dictionaries").Select("major_no, major_name,planned_enrollment,unplanned_enrollment").Where("major_no like ?", "%"+searchText+"%").Count(&total).Offset((page - 1) * pageSize).Limit(pageSize).Find(&major).Error
	return err, major, total
}

func UpdateMajorDic(c *gin.Context) (error, MajorDictionary) {
	var major MajorDictionary
	err := DB.Table("major_dictionaries").Where("major_no = ?", major.MNo).Updates(&major).Error
	return err, major
}

func InsertMajorDic(c *gin.Context) (error, MajorDictionary) {
	var major MajorDictionary
	c.BindJSON(&major)
	err := DB.Table("major_dictionaries").Select("major_no", "major_name", "planned_enrollment", "unplanned_enrollment").Create(&major).Error
	return err, major
}
